package agents.interaction;

import game.constants.ReferenceAgent;
import game.constants.ReferenceObject;
import game.constants.ReferenceSpecialFeature;
import interfaces.impl.HumanInteraction;
import objects.SpetialFeature;
import objects.WorldObject;
import universe.Universe;
import actions.ActionBuildStarcraftObject;
import actions.ActionXYtoMove;
import extra.ObjectId;
import extra.Position;

public class InteractionBuildRefinery extends HumanInteraction{
	
	private ActionXYtoMove actionXYtoMove;
	private ActionBuildStarcraftObject actionBuildStarcraftObject;
	
	private ObjectId objectId;
	
	SpetialFeature refineryPrice;
	SpetialFeature attackRange;
	
	SpetialFeature xToMove;
	SpetialFeature yToMove;

	@Override
	public void initializeParametersList() {
		objectId=new ObjectId();
		addParameter(objectId);
		
	}

	@Override
	public void initializeActionList() {
		actionXYtoMove= new ActionXYtoMove();
		addAction(actionXYtoMove);
		
		actionBuildStarcraftObject=new ActionBuildStarcraftObject();
		addAction(actionBuildStarcraftObject);
		
	}

	@Override
	public void initializeSpecialFeatures() {
		
		refineryPrice= new SpetialFeature(ReferenceSpecialFeature.SPETIAL_FEATURE_REFINERY_PRICE);
		addSpetialFeature(refineryPrice);
		
		attackRange= new SpetialFeature(ReferenceSpecialFeature.SPETIAL_FEATURE_ATTACK_RANGE);
		addSpetialFeature(attackRange);
		
		xToMove = new SpetialFeature(ReferenceSpecialFeature.SPETIAL_FEATURE_X_TO_MOVE);
		addSpetialFeature(xToMove);
		
		yToMove = new SpetialFeature(ReferenceSpecialFeature.SPETIAL_FEATURE_Y_TO_MOVE);
		addSpetialFeature(yToMove);
		
		
	}

	@Override
	public void initializeName() {
		name="Build Refinery";
		
	}

	@Override
	public void initializeId() {
		id=ReferenceAgent.AGENT_HUMAN_INTERACTION_BUILD_REFINERY;
		
	}

	@Override
	public void updateChild(WorldObject object, Universe universe, double time) {
		
		object.desactivateAllHumanInteractions();
		isActive=true;
		
		WorldObject vespeneGas= universe.getObject(objectId.getObjectId());
		if(vespeneGas==null){
			return ;
		}
		if(vespeneGas.getObjectReference()!=ReferenceObject.OBJECT_REFERENCE_VESPENE_GAS){
			return;
		}
		
		double distance = (Position.DistanceBetween(object.getPosition(), vespeneGas.getPosition()));
		
		if(distance>attackRange.getValue()){
			if(xToMove.getValue()==vespeneGas.getPosition().getX() && yToMove.getValue()==vespeneGas.getPosition().getY()){
				return;
			}
			actionXYtoMove.setSource(object);
			actionXYtoMove.setTarget(object);
			actionXYtoMove.setPosition(vespeneGas.getPosition());
			universe.setAction(actionXYtoMove);
			return;
		}	
		if(xToMove.getValue()==vespeneGas.getPosition().getX() && yToMove.getValue()==vespeneGas.getPosition().getY()){
			actionBuildStarcraftObject.setSource(object);
			actionBuildStarcraftObject.setTarget(object);
			actionBuildStarcraftObject.setPosition(vespeneGas.getPosition());
			actionBuildStarcraftObject.setPrice(refineryPrice.getValue());
			actionBuildStarcraftObject.setObjectReference(ReferenceObject.OBJECT_REFERENCE_REFINERY1);
			universe.setAction(actionBuildStarcraftObject);
			
			
			
			isActive=false;
			}
		
	}
	

}
